package Servlets;

import main.ServerGlobals;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import protocol.HTTPRequest;
import protocol.HTTPResponse;
import protocol.Headers;
import SQL.SQLLiteHandler;

public class add_user extends Servlet {

	protected boolean isError669(HTTPRequest httpRequest,
			ServerGlobals serverGlobals) {
		JSONArray rs = this.fSQLLiteHandler.Query("select "
				+ WhatsAppConstatnts.USERNAME
				+ " from "
				+ WhatsAppConstatnts.TABLE_GROUPS
				+ " where "
				+ WhatsAppConstatnts.GROUP_NAME
				+ "='"
				+ SQLLiteHandler.clean(httpRequest.getfHttpmsg().toJSONObject()
						.get("Target").toString())
				+ "' and "
				+ WhatsAppConstatnts.USERNAME
				+ " ='"
				+ SQLLiteHandler.clean(serverGlobals
						.getSession(this.getSessionId(httpRequest))
						.get(WhatsAppConstatnts.USERNAME).toString()) + "'");
		if (rs.size() == 0) {
			return true;
		}
		return false;
	}

	private boolean isError142(HTTPRequest httpRequest,
			ServerGlobals serverGlobals) {
		JSONArray rs = this.fSQLLiteHandler.Query("select "
				+ WhatsAppConstatnts.USERNAME
				+ " from "
				+ WhatsAppConstatnts.TABLE_GROUPS
				+ " where "
				+ WhatsAppConstatnts.GROUP_NAME
				+ "='"
				+ SQLLiteHandler.clean(httpRequest.getfHttpmsg().toJSONObject()
						.get("Target").toString())
				+ "' and "
				+ WhatsAppConstatnts.PHONE
				+ "='"
				+ SQLLiteHandler.clean(httpRequest.getfHttpmsg().toJSONObject()
						.get("User").toString()) + "'");
		if (rs.size() > 0) {
			return true;
		}
		return false;
	}

	protected boolean isError770(HTTPRequest httpRequest,
			ServerGlobals serverGlobals) {
		JSONArray rs = this.fSQLLiteHandler.Query("select "
				+ WhatsAppConstatnts.GROUP_NAME
				+ " from "
				+ WhatsAppConstatnts.TABLE_GROUPS
				+ " where "
				+ WhatsAppConstatnts.GROUP_NAME
				+ "='"
				+ SQLLiteHandler.clean(httpRequest.getfHttpmsg().toJSONObject()
						.get("Target").toString()) + "'");
		if (rs.size() == 0) {
			return true;
		}
		return false;
	}

	@Override
	public HTTPResponse process(HTTPRequest httpRequest,
			ServerGlobals serverGlobals) {

		if (!this.isCorrectMethod(httpRequest, Headers.POST)) {
			return new HTTPResponse(Headers.STATUS_405, Headers.HTTP_VERSION,
					new JSONObject(), "", false);
		}
		if (!this.isAuthorized(httpRequest, serverGlobals)) {
			return new HTTPResponse(Headers.STATUS_403, Headers.HTTP_VERSION,
					new JSONObject(), "", false);
		}
		if (!this.areParametersRequiredSent(new String[] { "Target", "User" },
				httpRequest)) {
			return new HTTPResponse(Headers.STATUS_200, Headers.HTTP_VERSION,
					new JSONObject(), WhatsAppConstatnts.ERROR_242, false);
		}

		if (isError770(httpRequest, serverGlobals)) {
			return new HTTPResponse(Headers.STATUS_200, Headers.HTTP_VERSION,
					new JSONObject(), WhatsAppConstatnts.ERROR_770, false);
		}

		if (isError669(httpRequest, serverGlobals)) {
			return new HTTPResponse(Headers.STATUS_200, Headers.HTTP_VERSION,
					new JSONObject(), WhatsAppConstatnts.ERROR_669, false);
		}

		if (isError142(httpRequest, serverGlobals)) {
			return new HTTPResponse(Headers.STATUS_200, Headers.HTTP_VERSION,
					new JSONObject(), WhatsAppConstatnts.ERROR_142, false);
		}

		JSONArray rs = this.fSQLLiteHandler.Query("select "
				+ WhatsAppConstatnts.USERNAME
				+ " from "
				+ WhatsAppConstatnts.TABLE_USERS
				+ " where "
				+ WhatsAppConstatnts.PHONE
				+ "='"
				+ SQLLiteHandler.clean(httpRequest.getfHttpmsg().toJSONObject()
						.get("User").toString()) + "'");
		for (int i = 0; i < rs.size(); i++) {
			try {
				this.fSQLLiteHandler.Query("INSERT INTO "
						+ WhatsAppConstatnts.TABLE_GROUPS
						+ " ("
						+ WhatsAppConstatnts.GROUP_NAME
						+ ","
						+ WhatsAppConstatnts.USERNAME
						+ ","
						+ WhatsAppConstatnts.PHONE
						+ ") values ('"
						+ SQLLiteHandler.clean(httpRequest.getfHttpmsg()
								.toJSONObject().get("Target").toString())
						+ "','"
						+ SQLLiteHandler.clean(((JSONObject) rs.get(i)).get(
								WhatsAppConstatnts.USERNAME).toString())
						+ "','"
						+ SQLLiteHandler.clean(httpRequest.getfHttpmsg()
								.toJSONObject().get("User").toString()) + "')");
			} catch (RuntimeException e) {
				if (e.getMessage().equals(SQLLiteHandler.SQL_CONCURRENCY)) {
					return new HTTPResponse(Headers.STATUS_200,
							Headers.HTTP_VERSION, new JSONObject(),
							WhatsAppConstatnts.ERROR_142, false);
				}
			}
		}

		return new HTTPResponse(Headers.STATUS_200, Headers.HTTP_VERSION,
				new JSONObject(), httpRequest.getfHttpmsg().toJSONObject()
						.get("User").toString()
						+ " added to "
						+ httpRequest.getfHttpmsg().toJSONObject()
								.get("Target").toString(), false);
	}
}
